var API = {

	config: {
		token: "5af9992437dbbe7c0aa2c1887ddaf73a",
		server: "http://api.ohemo.com/kata"
	},

	qiniu: {
	    Bucket: "pure-cms",
	    SignUrl: "token.php",
	    //qiniu test account
	    AK: "FBVGySr9T7jf13qLJtW3uaOLyhPbviD3yGVkxdsL",
	    SK: "3GosOuM_WColOWZuXlxcDCAUgUdV9vT4RyXMpfSY"
	},

	debug: true,
	//请求数据时，不展示loading
	ignoreLoading: ['like_feed', 'get_feed_list', 'get_push_count'],

	//二次封装的ajax请求
	emo_ajax: function(params) {
		var defaultBody = {
			type: 'POST',
			headers: {
				'X-Kata-Token' : this.config.token,
			},
			contentType: 'application/json; charset=utf-8'
		};
		var requestBody = $.extend({}, defaultBody, params);
		//添加Loading
		requestBody.beforeSend = (function(e,xhr,o) {
			
			var isIgnore = false;

			isIgnore = _.find(this.ignoreLoading, function(method) {
					return xhr.url.indexOf(method) != -1
			});

			if (isIgnore) return;

			if (!isIgnore && !$('#loading')[0]) {
				$('body').append('<div class="mask" id="loading"><div class="loader"></div></div>');
			}
		}).bind(this);
		//网络请求结束后 移除Loading动画
		requestBody.complete = function() {
			setTimeout(function() {
				$('#loading').remove();
			}, 10);
		}

		if (this.debug) {
			console.log("request:\n url:", requestBody.url, requestBody);
		}

		$.ajax(requestBody);
	},
	// 获取文章列表
	getFeedList: function(lastFeedId, success, error) {
		var url = this.config.server + "/f/get_feed_list.php";
		// var url = "mock/feed_list.json";
		this.emo_ajax({
			url: url,
			data: lastFeedId == 0 ? '' : JSON.stringify({'last_fid': lastFeedId}),
			success: success,
			error: error
		});
	},
	// 获取单个文章
	getFeed: function(fid, success, error) {
		var url = this.config.server + "/f/get_feed.php";
		this.emo_ajax({
			url: url,
			data: JSON.stringify({'fid': fid}),
			success: success,
			error: error
		});
	},
	// 发布一个文章
	publish: function(content, imageUrl, callback) {
		var url = this.config.server + '/f/publish.php';
		this.emo_ajax({
			url: url,
			data: JSON.stringify({
				"content": content,
				"image_url": imageUrl,
				"location": {
					// "area": "香港特別行政區,香港中文大學",
					"area": "广州市,海珠区",
					"latlng": "12714837, 2545871"
				},
				"direction": 0
			}),
			success: callback,
			error: callback
		});
	},
	// 上传图片
	uploadImage: function(fileEl, success) {
		var _this = this;
		this.uploadQiniu(fileEl, (function(data) {
			// var url = _this.config.server + '/upload_image_params.php';
			// this.emo_ajax({
			// 	url: url,
			// 	data: JSON.stringify({
			// 		'type': 'feed'
			// 	}),
			// 	success: success
			// });
			success(data)
		}).bind(this));
	},
	// 图片上传至七牛服务器
	uploadQiniu: function(fileEl, success) {
		var file = fileEl.files[0];
		// var token = "439iCgtbZxRe2KKiWijkb3u68hLmCjzI_4wvnboP:lN7VotuErPE-NIRlCulhwXxMU-0=:eyJzY29wZSI6ImVtby1pbWFnZTpmZWVkL2NmOTM1MDVlYjhlMTYzYTE5MTliZjQ1ZjdhNTJmN2VmLmpwZyIsImRlYWRsaW5lIjoxNDQxNjExMDQxLCJpbnNlcnRPbmx5IjoxfQ==";
		var fileName = 'feed/' + file.name;
		var token = genToken(this.qiniu.AK, this.qiniu.SK, {"scope": this.qiniu.Bucket + ":" + fileName,"deadline": parseInt(new Date().getTime()/1000 + 3600)});
		var formData = new FormData();
		formData.append("token", token);
		formData.append("key", fileName);
		formData.append("file", file);
		var _this = this;
		this.emo_ajax({
			url: 'http://upload.qiniu.com/',
			processData: false,
			contentType: false,
			data: formData,
			success: function(data) {
				var resp = data;
				success({
					'hash': resp.hash,
					'key': resp.key
				});
			}
		});
	},
	// 获取未读消息个数
	getPostCount: function(success) {
		var url = this.config.server + '/m/get_push_count.php';
		this.emo_ajax({
			url: url,
			success: success
		});
	},
	// 点赞
	like: function(fid, isLike, callback) {
		var url = this.config.server + "/f/like_feed.php";
		this.emo_ajax({
			url: url,
			data: JSON.stringify({"fid": fid,"is_like": isLike}),
			success: callback,
			error: callback
		});
	},
	// 评论
	comment: function(fid, text, success, error) {
		var url = this.config.server + '/comment/post.php';
		this.emo_ajax({
			url: url,
			data: JSON.stringify({'fid': fid, 'text': text}),
			success: success,
			error: error
		});
	},
	// 删除评论
	delComment: function(cid, success, error) {
		var url = this.config.server + '/comment/delete.php';
		this.emo_ajax({
			url: url,
			data: JSON.stringify({'cid': cid}),
			success: success,
			error: error
		});
	},
	// 获取文章评论列表
	getCommentList: function(fid, success, error) {
		var url = this.config.server + '/comment/list.php';
		// var url = "mock/comment_list.json";
		this.emo_ajax({
			url: url,
			data: JSON.stringify({"fid": fid,"last_cid":null}),
			success: success,
			error: error
		});
	},
	// 获取消息列表
	getMessageList: function(lastMid, success, error) {
		var url = this.config.server + '/m/get_message_list.php';
		this.emo_ajax({
			url: url,
			data: JSON.stringify({'last_mid': lastMid}),
			success: success,
			error: error
		});
	}
};